import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home/Home.vue'
import Logininfo from '../views/Login/components/logininfo.vue'
import Register from '../views/Login/components/register.vue'
import AddressList from '../views/Address/components/addressList.vue'
import AddressEdit from '../views/Address/components/addressEdit.vue'
import ReEdit from '../views/Address/components/reEdit.vue'
import http from '../serve/axios.js'

//引入vuex
import vuex from '../store/index.js'
Vue.use(VueRouter)

const routes = [{
		path: '/',
		name: 'Home',
		component: Home
	},
	{
		path: '/Sort',
		name: 'Sort',
		// route level code-splitting
		// this generates a separate chunk (about.[hash].js) for this route
		// which is lazy-loaded when the route is visited.
		component: () => import( /* webpackChunkName: "about" */ '../views/Sort/Sort.vue')
	},
	{
		path: '/Shopping',
		name: 'Shopping',
		//设置路由元信息
		meta: {
			yz: true
		},
		component: () => import('../views/Shopping/Shopping.vue')
	},
	{
		path: '/My',
		name: 'My',
		component: () => import('../views/My/My.vue')
	},
	{
		path: '/Cheack',
		name: 'Cheack',
		component: () => import('../views/Search/cheackPage.vue')
	},
	{
		path: '/Details',
		name: 'Details',
		component: () => import('../views/Details/Details.vue')
	},
	{
		path: '/Login',

		component: () => import('../views/Login/Login.vue'),
		children: [{
				path: '',
				component: Logininfo
			},
			{
				path: 'register',
				component: Register
			}
		]
	},
	{
		path: '/Order',
		name: 'Order',
		meta: {
			yz: true
		},
		component: () => import('../views/Order/Order.vue')
	},
	{
		path: '/Confirm',
		name: 'Confirm',
		meta: {
			yz: true
		},
		component: () => import('../views/Confirm/Confirm.vue')
	},
	{
		path: '/Address',
		meta: {
			yz: true
		},
		component: () => import('../views/Address/Address.vue'),
		children: [{
				path: '/',
				component: AddressList
			},
			{
				path: '/Edit',
				component: AddressEdit
			},
			{
				path: '/ReEdit',
				component: ReEdit
			}
		]
	},
]

const router = new VueRouter({
	base: process.env.BASE_URL,
	routes,
	//跳转路由让页面滚动条处于顶部
	scrollBehavior(to, from, savedPosition) {
		//if判断可加可不加、根据自己需求
		//savedPosition当且仅当通过浏览器的前进/后退按钮触发时才可用
		// if (savedPosition) {
		// 	return savedPosition
		// }
		return {
			x: 0,
			y: 0
		}
	}
})
router.beforeEach((to, from, next) => {

	//验证token
	// console.log(localStorage.getItem("token"))

	if (to.meta.yz) {
		// console.log(to,vuex.state.token)
		// console.log(to)
		if (vuex.state.token) {
			http.get('/address/route').then(res=>{
				if(res.data.message=='token验证成功'){
					'token验证成功'
				}else{
					localStorage.clear()
					vuex.state.message = '登陆注册'
					next({
						path: '/Login'
					})
				}
			})
		} else {
			next({
				path: '/Login'
			})
		}
	}
	next()
})


const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
	if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
	return originalPush.call(this, location).catch(err => err)
}


export default router
